<?php

// phpMyRealty 3
//
// File Name: reminder.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

include ( './config.php' );
include ( PATH . '/defaults.php' );

// ----------------------------------------------------------------------
// USER PASSWIRD REMINDER SECTION

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $lang['Password_Reminder'];

// Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

// If the Submit button was pressed we start this routine
if (isset($_POST['submit_reminder']) 
&& $_POST['submit_reminder'] == $lang['Password_Reminder_Submit'])
 {

  $form = array();

  // safehtml() all the POST variables
  // to insert into the database or
  // print the form again if errors
  // found
  $form = array_map('safehtml', $_POST);

  echo table_header ( $lang['Information'] );

  // Initially we think that no errors were found
  $count_error = 0;

  // Check for the empty or incorrect required fields
  if (empty($form['e_mail']) || strlen($form['e_mail']) < 4  || !valid_email($form['e_mail']))
   { echo $lang['Field_Empty'] . ' - <span class="warning">' . $lang['Realtor_e_mail'] . '</span><br />'; $count_error++;}

  // Check if this user exist
  $sql = 'SELECT * FROM ' . USERS_TABLE . ' WHERE email = "' . $form['e_mail'] . '" LIMIT 1';
  $r = $db->query($sql);
  $res = $db->numrows($r);

  if ($res < 1)
   { echo $lang['Password_Reminder_Not_Found'] . '<br />'; $count_error++;}

  if ($count_error > '0')
   echo '<br /><span class="warning">' . $lang['Errors_Found'] . ': ' . $count_error . '</span><br />';

  // If no errors were found during the above checks we continue
  if ($count_error == '0')
   {

    $f = $db->fetcharray($r);
    // Set new password, eg. "new924"
    $password = 'new';
    $password.= rand (1000, 9999);
    // Update user table with this new password
    $db->query('UPDATE ' . USERS_TABLE . ' SET password = "' . md5($password) . '" WHERE email = "' . $f['email'] . '" LIMIT 1');

//

    $mail = new PHPMailer();

    if(PHPMAILER == '3') {
     $mail->IsSMTP(); // set mailer to use SMTP
     $mail->Host = $smtp['host'];  // specify main and backup server
     $mail->SMTPAuth = true;     // turn on SMTP authentication
     $mail->Username = $smtp['login'];  // SMTP username
     $mail->Password = $smtp['password']; // SMTP password
    }
    elseif(PHPMAILER == '2') {
     $mail->IsSendmail(); // set mailer to use SMTP
    }
    else {
    }

    $mail->From = $conf['general_e_mail'];
    $mail->FromName = $conf['general_e_mail_name'];
    $mail->AddAddress($f['email']);

    $mail->Subject = $lang['Password_Reminder'];

    // Replacing the variable names
    $lang['Password_Reminder_Mail'] = str_replace('{name}', $f['first_name'] . ' ' . $f['last_name'], $lang['Password_Reminder_Mail']);
    $lang['Password_Reminder_Mail'] = str_replace('{login}', $f['login'], $lang['Password_Reminder_Mail']);
    $lang['Password_Reminder_Mail'] = str_replace('{password}', $password, $lang['Password_Reminder_Mail']);
    $lang['Password_Reminder_Mail'] = str_replace('{website_name}', $conf['website_name'], $lang['Password_Reminder_Mail']);

    $mail->MsgHTML    = $lang['Password_Reminder_Mail'];
    $mail->AltBody = removehtml($lang['Password_Reminder_Mail']);

    $mail->Send();

    $lang['Password_Reminder_Approved'] = str_replace('{email}', $f['email'], $lang['Password_Reminder_Approved']);
    // Show the thank you message
    echo $lang['Password_Reminder_Approved'];

   }
 
  echo table_footer ( );

 }

// If we open reminder.php for the first time
// or there were errors found in the form fields 
// we output the form again with the old variables 
// included
if (!isset($count_error) || $count_error > '0')
 {

  echo table_header ( $lang['Password_Reminder'] );

  // Define the form variables if the form is loaded for the first time
  if (!isset($form))
   {
    $form = array();
    $form['e_mail'] = '';
   }

  // Output the form 
  echo '
   <form action="' . URL . '/reminder.php" method="POST">
    <table width="100%" cellpadding="5" cellspacing="0" border="0">
       ';

  echo userform ($lang['Realtor_e_mail'], '<input type="text" size="45" name="e_mail" value="' . $form['e_mail'] . '" maxlength="30">', '1');

  echo userform ('', '<input type="Submit" class="submit" name="submit_reminder" value="' . $lang['Password_Reminder_Submit'] . '">');

  echo '
    </table>
   </form>
       ';

  echo table_footer ();
 }

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>